#pragma once

#include "noncopyable.h"
#include "Socket.h"
#include "Channel.h"

#include <functional>

class EventLoop;
class InetAddress;

class Acceptor : noncopyable
{
public:
    using NewConnectionCallback = std::function<void(int sockfd, const InetAddress &)>;

    Acceptor(EventLoop *loop, const InetAddress &listenAddr, bool reuseport);
    ~Acceptor();

    void setNewConnectionCallback(const NewConnectionCallback &cb) { newConnectionCallback_ = cb; }
    void listen();
    bool listenning() { return listenning_; }

private:
    // listen套接字的读事件处理函数
    void handleRead();

    // baseloop只负责监听连接。mainloop
    EventLoop *loop_;

    // listensock监听套接字
    Socket acceptSocket_;
    // listensockChannel，给baseloop的poller对其监听
    Channel acceptChannel_;
    // 对新连接的socket进行处理
    NewConnectionCallback newConnectionCallback_;
    bool listenning_;
};